package com.banmao.mall.controller.client;

import com.banmao.mall.model.bo.client.goods.GoodsListBO;
import com.banmao.mall.model.vo.BaseRespVO;
import com.banmao.mall.model.vo.client.goods.GoodsCategoryVO;
import com.banmao.mall.model.vo.client.goods.GoodsDetailVO;
import com.banmao.mall.model.vo.client.goods.GoodsListVO;
import com.banmao.mall.model.vo.client.goods.GoodsRelatedVO;
import com.banmao.mall.model.vo.client.index.GoodsCountVO;
import com.banmao.mall.model.vo.client.index.GoodsVO;
import com.banmao.mall.model.vo.client.index.HomeIndexVO;
import com.banmao.mall.service.client.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController("cGoodsController")
@RequestMapping("/wx")
public class GoodsController {

    @Autowired
    GoodsService goodsService;

    // 获取首页信息
    @RequestMapping("/home/index")
    public BaseRespVO homeIndex() {

        HomeIndexVO homeIndexVO = goodsService.index();
        if(homeIndexVO == null) {
            return BaseRespVO.error("服务器故障", 401);
        }

        return BaseRespVO.ok(homeIndexVO);
    }


    // 获取商品总数
    @RequestMapping("/goods/count")
    public BaseRespVO goodsCount() {

        int goodsCount = goodsService.goodsCount();
        GoodsCountVO goodsCountVO = new GoodsCountVO();
        goodsCountVO.setGoodsCount(goodsCount);

        return BaseRespVO.ok(goodsCountVO);
    }


    // 根据商品类目获取信息
    @RequestMapping("/goods/category")
    public BaseRespVO goodsCategory(@RequestParam("id") Integer id) {

        GoodsCategoryVO goodsCategoryVO = goodsService.goodsCategory(id);
        if(goodsCategoryVO == null) {
            return BaseRespVO.error("服务器出现了小问题", 401);
        }

        return BaseRespVO.ok(goodsCategoryVO);
    }


    // 根据二级类目ID查找商品并分页
    @RequestMapping("/goods/list")
    public BaseRespVO goodsList(GoodsListBO goodsListBO) {

        GoodsListVO goodsListVO = goodsService.goodsList(goodsListBO);
        if(goodsListVO == null) {
            return BaseRespVO.error("服务器出现了小问题", 401);
        }

        return BaseRespVO.ok(goodsListVO);
    }


    // 商品详情
    @RequestMapping("/goods/detail")
    public BaseRespVO goodsDetail(@RequestParam("id") Integer goodsId) {

        GoodsDetailVO goodsDetailVO = goodsService.goodsDetail(goodsId);
        if(goodsDetailVO == null) {
            return BaseRespVO.error("服务器出问题啦~", 401);
        }
        return BaseRespVO.ok(goodsDetailVO);
    }


    // 获取相关商品
    @RequestMapping("/goods/related")
    public BaseRespVO goodsRelated(@RequestParam("id") Integer goodsId) {

        GoodsRelatedVO goodsRelatedVO = goodsService.goodsRelated(goodsId);
        if(goodsRelatedVO == null) {
            return BaseRespVO.error("服务器抽风啦！", 401);
        }

        return BaseRespVO.ok(goodsRelatedVO);
    }

}
